package com.ica.android.ui.contact;

import java.util.ArrayList;

import com.ica.R;
import com.ica.android.ui.contact.util.AndroidContact;
import com.ica.android.ui.contact.util.ContactArrayAdapter;
import com.ica.model.facade.ContactFacadeMockImpl;

import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * @author   Hector
 */
public abstract class ContactGroupActivity extends Activity {
	
	/**
	 */
	protected ListView lv = null;
	/**
	 */
	protected EditText etName = null;
	/**
	 */
	protected ArrayList<AndroidContact> contacts = null;
	/**
	 */
	protected boolean[] checkedContacts;
	/**
	 * @uml.property  name="adapter"
	 * @uml.associationEnd  
	 */
	protected ContactArrayAdapter adapter = null;
	/**
	 */
	protected Runnable viewContent = null;
	/**
	 */
	protected ProgressDialog progressDialog = null;
	/**
	 * @uml.property  name="contactFacade"
	 * @uml.associationEnd  
	 */
	protected final ContactFacadeMockImpl contactFacade = ContactFacadeMockImpl.getInstance();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	protected void init(int layout) {
        setContentView(layout);
        
        Button save = (Button) findViewById(R.id.bSaveContactGroup);
        save.setOnClickListener(onClickSave());     
        contacts = new ArrayList<AndroidContact>();
        etName = (EditText) findViewById(R.id.etContactGroupName);
        adapter = new ContactArrayAdapter(this, R.layout.contacts_list_view, contacts);
        lv = (ListView) findViewById(R.id.lvContacts);
        lv.setAdapter(adapter);     
        viewContent = new Runnable() {
        	public void run() {
        		populateContactList();
			}
		};	
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {			
				AndroidContact c = contacts.get(pos);
				c.setChecked(!c.getChecked());
			}	
		});	
		Thread thread = new Thread(null,viewContent,"LoadContacts");
		thread.start();
		progressDialog = ProgressDialog.show(this, "Wait", "Loading contacts...",true);
	}
	
	protected abstract OnClickListener onClickSave();
	
	protected ArrayList<AndroidContact> getSelectedContacts() {		
		ArrayList<AndroidContact> res = new ArrayList<AndroidContact>();
		for(int i=0;i<contacts.size();i++) {		
			if (contacts.get(i).getChecked())
				res.add(contacts.get(i));
		}
		return res;
	}
	
	private String removeChar(String s, char c) {
	   String r = "";
	   for (int i = 0; i < s.length(); i ++) {
	      if (s.charAt(i) != c) r += s.charAt(i);
	   }
	   return r;
	}
	
    protected void populateContactList() { 	
    	Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
    	while (phones.moveToNext())
    	{
    	  long id = phones.getLong(phones.getColumnIndex(ContactsContract.Contacts._ID));
    	  String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    	  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    	  AndroidContact contact = new AndroidContact(id,name,removeChar(phoneNumber,'-'));    	 
		  contacts.add(contact);
    	}
    	phones.close();
    	checkedContacts = new boolean[contacts.size()];
		runOnUiThread(getAfterFillList());
    }
    
    protected Runnable getAfterFillList() {  	
    	return new Runnable() { 		
    		public void run() {
    			if(contacts != null && contacts.size() > 0){
    	            adapter.notifyDataSetChanged();
    	        }
    	        progressDialog.dismiss();		
    		}
    	};
    }
}
